import { request } from 'umi';
import notify from 'devextreme/ui/notify';
import getApiData from '@/services/cpasRequest';
import { nanoid10 } from '@/utils';

export async function deleteAttach(fjids: any[], dbname: string, zcbfid: string, sjxmbh: string | undefined): Promise<any> {
  // 删除附件
  const res = await request('/cpasApi/draftFjManage/removeFjs', {
    method: 'POST',
    data: {
      dbname: dbname,
      fjids: fjids,
      distPath: `${sjxmbh}/${zcbfid}`,
    },
  });
  if (res.ok) {
    notify('删除成功', 'success', 1000);
    return true;
  } else {
    notify('删除失败', 'error', 1000);
    return false;
  }
}


//提取路径最后名称,和后缀
function extractFileNameAndExtension(path: string) {
  let separator = '/';
  if (path.includes('\\')) {
    separator = '\\';
  }

  // 根据路径分隔符分割路径
  const parts = path.split(separator);

  // 获取最后一个部分
  let fileName = parts[parts.length - 1];

  // 分离文件名和后缀
  const lastDotIndex = fileName.lastIndexOf('.');
  let fileExtension = '';

  if (lastDotIndex !== -1) {
    fileExtension = fileName.substring(lastDotIndex + 1);
    fileName = fileName.substring(0, lastDotIndex);
  }

  return { fileName, fileExtension };
}

export async function importAttachFromPlatform(
  // 从附件平台导入附件
  filePaths: any[],
  dbname: string,
  zcbfid: string,
  username: string | undefined,
  usercode: string,
  type: string = '0',
  sjxmbh: string | undefined,
  dgid?: string,
  pLinkID?: string,
  rnames?: any,

): Promise<any> {
  // 处理filePaths
  const filePathsArr = filePaths.map((item: any) => {
    return item.replace(/\\/g, '\\\\');
  });
  if (filePathsArr.length === 0) {
    notify('请选择文件', 'error', 1000);
    return false;
  }
  let sendData: any = dgid
    ? {
      type: type,
      sourceFiles: filePathsArr,
      user: username,
      distPath: `${sjxmbh}/${zcbfid}`,
      userCode: usercode,
      dgid: dgid,
      dbname: dbname,
      plinkid: pLinkID
    } : {
      type: type,
      sourceFiles: filePathsArr,
      user: username,
      distPath: `${sjxmbh}/${zcbfid}`,
      userCode: usercode,
      dbname: dbname
    }
  if (Array.isArray(rnames)) {
    sendData = { ...sendData, rnames }
  }


  const res = await request('/cpasApi/draftFjManage/createFjByExternal', {
    method: 'POST',
    data: sendData,
  });

  const params = {
    data: res.data,
    success: res.ok,
  };

  return Promise.resolve(params);

}

export async function replaceAttach(
  // 替换附件
  paramData: {
    filePaths: any[],
    fjids: any[],
    dbname: string,
    zcbfid: string,
    sjxmbh: string | undefined
  }
): Promise<any> {
  const res = await request('/cpasApi/draftFjManage/replaceFjs', {
    method: 'POST',
    data: {
      dbname: paramData.dbname,
      fjids: paramData.fjids,
      sourceFile: paramData.filePaths[0],
      distPath: `${paramData.sjxmbh}/${paramData.zcbfid}`,
    },
  });

  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });

  // if (res.ok) {
  //   notify('替换成功', 'success', 2000);
  //   return true;
  // } else {
  //   notify(res.msg, 'error', 2000);
  //   return false;
  // }
}


// 获取底稿列表
export async function detailDelList(params: {
  dbname: string;
  fjid: string;
}) {
  const res = await getApiData("sjdg.fj.linkInfo", params);
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}


// // 获取附件属性
export async function getFileProperties(dbname: string, fjid: string): Promise<any> {
  const res = await request('/cpasApi/draftFjManage/getFileProperties', {
    method: 'GET',
    params: {
      dbname: dbname,
      fjid: fjid,
    }
  })
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}

// 附件重命名
export async function fjidRename(
  paramData: {
    sjxmbh: string,
    dbname: string,
    zcbfid: string,
    fjid: string,
    rename: string,
    refjlx: string
  }
): Promise<any> {
  const res = await request('/cpasApi/draftFjManage/fjidRename', {
    method: 'POST',
    data: paramData,
  });

  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}

// 解除关联
export async function fjUnchain(
  dbname: string,
  dgid: string,
  fjid: string,
): Promise<any> {
  const res = await request('/cpasApi/draftFjManage/fjUnchain', {
    method: 'POST',
    data: {
      dbname: dbname,
      dgid: dgid,
      fjid: fjid,
    },
  });
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}



// 附件导出 
export async function exportFjs(
  sjxmbh: string,
  dbname: string,
  fjNames: any,
  distDir: any
): Promise<any> {
  const res = await request('/cpasApi/draftFjManage/exportFjs', {
    method: 'POST',
    data: {
      sjxmbh,
      dbname,
      fjNames: fjNames,
      distDir: distDir,
    },
  });
  return Promise.resolve({
    data: res.data,
    success: res.ok,
  });
}
